package com.mocai.merchant.controller;


import com.mocai.merchant.core.domain.Page;
import com.mocai.merchant.core.mapper.BaseMapper;
import com.mocai.merchant.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 基础的控制层，集成增删改查基础接口
 *
 * @param <S>  业务Service
 * @param <M>  持久化Mapper
 * @param <E>  实体Entity
 * @param <ID> 实体主键ID
 */
public abstract class BaseController<S extends BaseService<M, E, ID>, M extends BaseMapper<E, ID>, E, ID extends Serializable> {
    // 视图URL
    private String viewUrl;

    // 业务层接口
    @Autowired
    S service;

    /**
     * 获取具体子类的RequestMapping注解的值，作为视图的URL
     */
    public BaseController() {
        Class<? extends BaseController> cls = this.getClass();
        RequestMapping requestMapping = cls.getAnnotation(RequestMapping.class);
        if (null != requestMapping) {
            this.viewUrl = requestMapping.value()[0];
        }
    }

    /**
     * 跳转到视图界面
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String view() {
        return viewUrl;
    }

    /**
     * 根据ID查询
     *
     * @param id 主键
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object getById(@PathVariable("id") ID id) {
        return service.find(id);
    }

    /**
     * 分页查询
     *
     * @param pageNumber 第几页
     * @param pageSize   每页记录数
     * @return
     */
    @RequestMapping(value = "/page/{pageNumber:[0-9]\\d*}/{pageSize:[1-9]\\d*}", method = RequestMethod.GET)
    @ResponseBody
    public Object getForPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
        return service.findPage(new Page(pageNumber, pageSize));
    }

    /**
     * 根据ID删除
     *
     * @param id 主键
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteById(@PathVariable("id") ID id) {
        return service.delete(id);
    }

    /**
     * 根据ID批量删除
     *
     * @param ids 主键数组
     * @return
     */
    @RequestMapping(value = "/delete/batch/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteBatch(@PathVariable("ids") ID[] ids) {
        return service.delete(Arrays.asList(ids));
    }

    /**
     * 更新
     *
     * @param entity 实体对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody E entity) {
        return service.update(entity);
    }

    /**
     * 批量更新
     *
     * @param entities
     * @return
     */
    @RequestMapping(value = "/update/batch", method = RequestMethod.POST)
    @ResponseBody
    public Object updateBatch(@RequestBody List<E> entities) {
        return service.update(entities);
    }

    /**
     * 新增
     *
     * @param entity 实体对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody E entity) {
        return service.save(entity);
    }

    /**
     * 批量新增
     *
     * @param entities 实体对象
     * @return
     */
    @RequestMapping(value = "/add/batch", method = RequestMethod.POST)
    @ResponseBody
    public Object addBatch(@RequestBody List<E> entities) {
        return service.save(entities);
    }


}
